/*
 * Author: Patrick 
 * Date: Nov 16, 2013
 */
$(function () {
    var successMsgContainer = $("div[id*=successMessageContainer]");
    var errorMsgContainer = $("div[id*=errorMessageContainer]");
    var successMsg = $("span[id*=successMessage]");
    var errorMsg = $("span[id*=errorMessage]");
    var buttonEdit = $("button[id*=editCourse]");
    var buttonDelete = $("button[id*=deleteCourse]");
    buttonEdit.click(setSelectedCourseValues);
    buttonDelete.click(deleteCourse);
    
    var $coursesGrid = $("#coursesGrid");
    
    if (successMsg.text()) successMsgContainer.show();
    if (errorMsg.text()) errorMsgContainer.show();
    
    loadCourses();
    $coursesGrid.bind('rowselect', function (event) {
        var rowindex = event.args.rowindex;
        
        if (rowindex > -1) {
            buttonEdit.removeClass("disabled");
            buttonDelete.removeClass("disabled");
            buttonEdit.removeAttr("disabled");
            buttonDelete.removeAttr("disabled");
        }
        else {
            buttonEdit.class("disabled");
            buttonDelete.class("disabled");
            buttonEdit.attr("disabled", "disabled");
            buttonDelete.attr("disabled", "disabled");
        }
    });
    
    function setSelectedCourseValues() {
        var rowindex = $coursesGrid.jqxGrid('getselectedrowindex');
        var datarow = $coursesGrid.jqxGrid('getrowdata', rowindex);
        $("input[id*=inputCourseId]").val(datarow.CourseId);
        $("input[id*=inputCourseCode]").val(datarow.CourseCode);
        $("input[id*=inputCourseName]").val(datarow.CourseName);
        $("input[id*=inputCreditHours]").val(datarow.CreditHours);
    }
    
    function loadCourses() {
        // prepare the data
        var source =
        {
            datatype: "json",
            datafields: [
                { name: 'CourseId', type: 'int' },
                { name: 'CourseCode', type: 'string' },
                { name: 'CourseName', type: 'string' },
                { name: 'CreditHours', type: 'int' }
            ],
            data: { action: "loadCourses" },
            url: "./adminCourses"
        };

        var dataAdapter = new $.jqx.dataAdapter(source,
            {
                formatData: function (data) {
                    $.extend(data, {
                        featureClass: "P",
                        style: "full",
                        maxRows: 50
                    });

                    return data;
                }
            }
        );

        var dataAdapter = new $.jqx.dataAdapter(source, {
            downloadComplete: function (data, status, xhr) { },
            loadComplete:  function (data) {
                        return $.makeArray(data);
                    },
            loadError: function (xhr, status, error) { }
        });
        // initialize jqxGrid
        $coursesGrid.jqxGrid(
        {
            width: 700,
            source: dataAdapter,
            theme: 'base',
            pageable: true,
            autoheight: true,
            sortable: true,
            altrows: true,
            enabletooltips: true,
            editable: false,
            columns: [
              { text: 'Course Code', datafield: 'CourseCode' },
              { text: 'Course Name', datafield: 'CourseName' },
              { text: 'Credit Hours', datafield: 'CreditHours' }
            ]
        });
    }
    
    function deleteCourse() {
        if (confirm("Are you sure you want to delete this course? \nAll sections associated with this course will be deleted as well.")) {
            setSelectedCourseValues();
            $("button[id*=submitDeleteCourse]").click();
        }
    }
});
